<?php
declare (strict_types=1);

namespace app\middleware;

use app\util\ReturnCode;
// use think\facade\Cache;

class ApiLogin
{
    public function handle($request, \Closure $next)
    {
        $token = $request->header('token');
        if (!$token) {
            return json([
                'code' => ReturnCode::AUTH_ERROR,
                'msg' => '未登录或登录已过期',
                'data' => null
            ]);
        }

        // 从缓存中获取用户信息
        // $userInfo = Cache::get($token);
        $userInfo = getCache($token);
        if (!$userInfo || !isset($userInfo['id'])) {
            return json([
                'code' => ReturnCode::SESSION_TIMEOUT,
                'msg' => '登录已过期',
                'data' => null
            ]);
        }

        // 将用户信息注入到请求中
        $request->userInfo = $userInfo;
        
        return $next($request);
    }
} 